<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>bash_profile和bashrc区别 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>bash_profile和bashrc区别</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux/">Linux</a>
			</span>
		
	</div>

	

	
		<h2 id="bash-profile-与-bashrc-的区别"><a href="#bash-profile-与-bashrc-的区别" class="headerlink" title=".bash_profile 与 .bashrc 的区别"></a>.bash_profile 与 .bashrc 的区别</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>.bash_profile</code> is executed for <code>login shells</code>, while <code>.bashrc</code> is executed for interactive <code>non-login shells</code>.</p>
<h2 id="login-shell-与-non-login-shell-的区别"><a href="#login-shell-与-non-login-shell-的区别" class="headerlink" title="login shell 与 non-login shell 的区别"></a>login shell 与 non-login shell 的区别</h2><ol>
<li>当你直接在机器login界面登陆、使用ssh登陆或者su切换用户登陆时，.bash_profile 会被调用来初始化shell环境</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Note：.bash_profile文件默认调用.bashrc文件</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.bash_profile中有如下内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> [ -f ~/.bashrc ]; <span class="keyword">then</span></div><div class="line">        . ~/.bashrc</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<ol>
<li><p>当你不登陆系统而使用ssh直接在远端执行命令，.bashrc 会被调用</p>
</li>
<li><p>当你已经登陆系统后，每打开一个新的Terminal时，.bashrc 都会被再次调用。</p>
</li>
</ol>
<h2 id="测试准备工作"><a href="#测试准备工作" class="headerlink" title="测试准备工作"></a>测试准备工作</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;hclient2主机hadoop用户家目录下执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[hadoop@hclient2 ~]$ <span class="built_in">echo</span> <span class="string">"invoke hclient2:~/.bashrc"</span>&gt;&gt;.bashrc</div><div class="line">[hadoop@hclient2 ~]$ <span class="built_in">echo</span> <span class="string">"invoke hclient2:~/.bash_profile"</span>&gt;&gt;.bash_profile</div></pre></td></tr></table></figure>
<h3 id="Login-Shell"><a href="#Login-Shell" class="headerlink" title="Login Shell"></a>Login Shell</h3><ol>
<li>窗口登陆</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">Red Hat Enterprise Linux Server release 6.3 (Santiago)</div><div class="line">Kernel 2.6.32-279.el6.x86_64 on an x86_64</div><div class="line"></div><div class="line"></div><div class="line">hclient2 login: hadoop</div><div class="line">Password:</div><div class="line">Last login: Mon Feb 25 23:03:45 on tty1</div><div class="line">invoke hclient2:~/.bashrc</div><div class="line">invoke hclient2:~/.bash_profile</div><div class="line"></div><div class="line">[hadoop@hclient2 ~]$</div></pre></td></tr></table></figure>
<ol>
<li>SSH 登陆</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[hadoop@hserver ~]$ ssh hclient2</div><div class="line">Last login: Mon Feb 25 22:42:19 2013 from hserver</div><div class="line">invoke hclient2:~/.bashrc</div><div class="line">invoke hclient2:~/.bash_profile</div><div class="line">[hadoop@hclient2 ~]$</div></pre></td></tr></table></figure>
<ol>
<li>su 登陆</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@hclient2 ~]<span class="comment"># su - hadoop</span></div><div class="line">invoke hclient2:~/.bashrc</div><div class="line">invoke hclient2:~/.bash_profile</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Non-login Shell</strong>:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Note: </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ssh ...[user@] hostname [<span class="built_in">command</span>]</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">If <span class="built_in">command</span> is specified, it is executed on the remote host instead of a login shell.</div><div class="line">[hadoop@hserver ~]$ ssh hclient2 hostname</div><div class="line">invoke hclient2:~/.bashrc</div><div class="line">hclient2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>注</strong>：若要配置环境变量之类，最保险是写在 .bashrc 文件中。因为不管是登陆还是不登陆，该文件总会被调用！</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/1. Linux 基础/43.bash_profile和bashrc区别/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
